package com.jnsw.gps;

import android.content.Context;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 点过滤
 * Created by Administrator on 2017/3/14 0014.
 */
public class ChooseThread extends Thread {
    List<GPSPoint> GpsList = new ArrayList<>();
    GPSPoint cPoint = null;
Context context;
    public ChooseThread(Context context) {
        this.context = context;
    }

    public ChooseThread(Context context,List<GPSPoint> GpsList) {
        super();
        this.context = context;
        this.GpsList = GpsList;
    }

    @Override
    public void run() {
        if (GpsList.size() == 5) {
            List<Double> dlist = new ArrayList<>();
            double d0 = getDistance(GpsList.get(0), GpsList.get(1));
            double d1 = getDistance(GpsList.get(0), GpsList.get(2));
            double d2 = getDistance(GpsList.get(0), GpsList.get(3));
            double d3 = getDistance(GpsList.get(0), GpsList.get(4));
            double d4 = getDistance(GpsList.get(1), GpsList.get(2));
            double d5 = getDistance(GpsList.get(1), GpsList.get(3));
            double d6 = getDistance(GpsList.get(1), GpsList.get(4));
            double d7 = getDistance(GpsList.get(2), GpsList.get(3));
            double d8 = getDistance(GpsList.get(2), GpsList.get(4));
            double d9 = getDistance(GpsList.get(3), GpsList.get(4));
            dlist.add(d0);
            dlist.add(d1);
            dlist.add(d2);
            dlist.add(d3);
            dlist.add(d4);
            dlist.add(d5);
            dlist.add(d6);
            dlist.add(d7);
            dlist.add(d8);
            dlist.add(d9);
            double mind = Collections.min(dlist);
            int index = dlist.indexOf(mind);
            if (index == 0) {
                cPoint = GpsList.get(0);
            } else if (index == 1) {
                cPoint = GpsList.get(0);
            } else if (index == 2) {
                cPoint = GpsList.get(0);
            } else if (index == 3) {
                cPoint = GpsList.get(0);
            } else if (index == 4) {
                cPoint = GpsList.get(1);
            } else if (index == 5) {
                cPoint = GpsList.get(1);
            } else if (index == 6) {
                cPoint = GpsList.get(1);
            } else if (index == 7) {
                cPoint = GpsList.get(2);
            } else if (index == 8) {
                cPoint = GpsList.get(2);
            } else if (index == 9) {
                cPoint = GpsList.get(3);
            }


            if (cPoint != null) {
                Log.d("chooethread-------", cPoint.x + "--=-=-=-=-" + cPoint.y);
               EventBus.getDefault().post(new ChooseMessage(cPoint));//定位信息
            }
        }
    }


    //经纬度点 求距离
    public static double getDistance(GPSPoint p1, GPSPoint p2) {
        double radLat1 = p1.x * Math.PI / 180;
        double radLat2 = p2.x * Math.PI / 180;
        double a = radLat1 - radLat2;
        double b = p1.y * Math.PI / 180 - p2.y * Math.PI / 180;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)
        s = Math.round(s * 10000) / 10000;
        return s;
    }
}
